/*
 * author      : 吴中勤 
 * date        : 2018/9/4
 * description : 事件订阅
 */

package com.deepexi.adapter.mq;

import com.deepexi.mq.api.IMQService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class RocketMQReceiver {

    private static final Logger logger = LoggerFactory.getLogger(RocketMQReceiver.class);

    private static final boolean MQ_SWITCH = true;


    @Autowired
    private MessageHandler messageHandler;

    @Autowired
    private IMQService mqService;

    private static final String tagName = "WORKFLOW";

    @PostConstruct
    public void subscribe() {
        if (!MQ_SWITCH) {
            logger.warn("关闭mq消息订阅");
        }

        /* 进行订阅 */
        for (TopicEnum topicEnum : TopicEnum.values()) {
            String topicName = topicEnum.topic;
            mqService.createConsumer().subscribe(topicName, tagName, new EventProcessorImpl(topicName, messageHandler));
            logger.info("订阅: tag={}, desc={}, topic={}", tagName, topicEnum.desc, topicName);
        }
    }
}
